<?php
class TF_Form_Element_Antispam extends Zend_Form_Element_Text
{
    protected $_word = 'captcha me';
    /**
     * Set captcha adapter
     *
     * @param string|array|Zend_Captcha_Adapter $captcha
     * @param array $options
     */
    public function __construct ($spec, $options = null)
    {
        parent::__construct($spec, $options);
        if ($options['word']) {
            $this->_word = $options['word'];
        }
        $this->setAllowEmpty(true)->setRequired(true)->setAutoInsertNotEmptyValidator(false)->addValidator(
            new Zend_Validate_Identical($this->_word), true);
    }
    public function render (Zend_View_Interface $view = null)
    {
    	$decorators = $this->getDecorators();
    	$decoratorFound = false;
    	foreach($decorators as $decorator){
    		if($decorator instanceof Zend_Form_Decorator_Description){
    			$decoratorFound = true;
    			break;
    		}
    	}
    	if(!$decoratorFound){
    		$this->addDecorator('Description',array('escape' => false));
    	}
    	$this->setDescription('<div id="spam' . $id . 'Div">Vyplňte prosím do tohoto políčka "' . $this->_word .
             '" - jedná se o ochranu proti robotům zasílajícím spam. </div>');
        $html = parent::render($view);
        $id = $this->getName();
        $v = $view;
        $v->jQuery()->enable();
        $v->jQuery()->addOnLoad("$('#spam{$id}Div').hide();");
        $v->jQuery()->addOnLoad("$('#{$id}').val('$this->_word');");
        return $html;
    }
}